$(function () {
    $("#jqGrid").Grid({
        url: '../devparameter/list',
        rownumWidth: 45,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '厂商', name: 'faName', index: 'FA_NAME', width: 80},
            {label: '厂商编码', name: 'faCode', index: 'FA_CODE', width: 50},
            {label: '设备类型', name: 'devTypeName', index: 'DEV_TYPE_NAME', width: 40},
            {label: '设备类别编码', name: 'devType', index: 'DEV_TYPE', width: 40},
            {label: '参数名称', name: 'paramName', index: 'PARAM_NAME', width: 40},
            {label: '参数描述', name: 'paramDesc', index: 'PARAM_DESC', width: 90},
            {label: '参数阈值下限', name: 'thresholdMin', index: 'THRESHOLD_MIN', width: 40},
            {label: '参数阈值上限', name: 'thresholdMax', index: 'THRESHOLD_MAX', width: 40}
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devParamModel: {devTypeName:'', paramDesc:'', paramName:'', faName:'', faCode:'', devType:'', id:'', thresholdMax:0,thresholdMin:0  },
        ruleValidate: {
            faName: [
                {required: true, message: '厂商必填', trigger: 'blur'}
            ],
            devType: [
                {required: true, message: '设备类型必填', trigger: 'blur'}
            ],
            paramName: [
                {required: true, message: '参数名称必填', trigger: 'blur'}
            ],
            paramDesc: [
                {required: true, message: '参数描述必填', trigger: 'blur'}
            ],
            thresholdMin: [
                {required: true, message: '参数阈值下限必填', trigger: 'blur'}
            ],
            thresholdMax: [
                {required: true, message: '参数阈值上限必填', trigger: 'blur'}
            ]
        },
        q: {
            faName: '',
            devType: '', // 设备类型
            paramDescKey: '' // 参数描述关键字
        }
    },
    methods: {
        // 加载工场类别
        loadFacType:function(){
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择厂家',
                content: '../sys/factorytype_choose.html?type=0',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getFacType();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.devParamModel.faCode = res.faCode;
                        vm.devParamModel.faName = res.faName;
                    }
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devParamModel = {devTypeName:'', delFlag:'', paramDesc:'', paramName:'', faName:'', faCode:'', devType:'', id:'', };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.devParamModel.id == (null || '') ? "../devparameter/save" : "../devparameter/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devParamModel),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null){
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devparameter/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function(id){
            Ajax.request({
                url: "../devparameter/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.devParamModel = r.devParameter;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function() {
            vm.q = {
                faName: '',
                devType: '', // 设备类型
                paramDescKey: '' // 参数描述关键字
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (!res.err) {
                this.$Loading.finish();
                alert('导入成功', function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
    }
});
